package orange.com.openstack.sahara.api;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;

import com.jayway.jsonpath.JsonPath;

import orange.com.openstack.sahara.httpConnection.RestCall;

/**
 * class allowing the interaction with the JobExecutions Rest service
 * @author osvaldo.cocucci@gmail.com
 * 
 */
public class JobExecutions {
	String endpoint;
	String tenantId;
	String token;

	/**
	 * @param endpoint
	 * @param tenantId
	 * @param token
	 * 
	 *            constructor
	 */
	public JobExecutions(String endpoint, String tenantId, String token) {

		this.endpoint = endpoint;
		this.tenantId = tenantId;
		this.token = token;
	}

	/**
	 * 
	 * This operation returns the list of all Job Executions. This operation
	 * does not require a request body.
	 */
	public void listJobExecutions() {
		RestCall caller = new RestCall();
		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/job-executions", null, "GET", token);
		try {
			connection.connect();
			System.out.println(caller.getResponse(connection));
			connection.disconnect();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param jobExecutionId
	 *            This operation shows the information about a specified Job
	 *            Execution. This operation does not require a request body.
	 */
	public void showJobExecution(String jobExecutionId) {
		RestCall caller = new RestCall();
		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/job-executions/" + jobExecutionId, null, "GET", token);
		try {
			connection.connect();
			System.out.println(caller.getResponse(connection));
			connection.disconnect();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param jobExecutionId
	 *            This operation shows the status information about a specified Job
	 *            Execution. This operation does not require a request body.
	 */
	public String showJobExecutionStatus(String jobExecutionId) {
		RestCall caller = new RestCall();
		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/job-executions/" + jobExecutionId + "/refresh-status",
				null, "GET", token);
		try {
			if (connection.getResponseCode() != 200)
				return null;
			String JSONreponse = caller.getResponse(connection);
			return JsonPath.read(JSONreponse, "$.job_execution.info.status");

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @param jobExecutionId
	 *            This operation shows the progress information about a specified Job
	 *            Execution. This operation does not require a request body.
	 */
	public String showJobExecutionProgress(String jobExecutionId) {
		RestCall caller = new RestCall();
		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/job-executions/" + jobExecutionId + "/refresh-status",
				null, "GET", token);
		try {
			if (connection.getResponseCode() != 200)
				return null;
			String JSONreponse = caller.getResponse(connection);
			return JsonPath.read(JSONreponse, "$.job_execution.progress");

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param jobExecutionId
	 *            This operation cancels specified Job Execution. This operation
	 *            does not require a request body.
	 */
	public boolean cancelJobExecution(String jobExecutionId) {
		RestCall caller = new RestCall();
		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/job-executions" + jobExecutionId + "/cancel", null, "GET",
				token);
		try {
			if (connection.getResponseCode() == 200)
				return true;
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * @param jobExecutionId
	 *            Remove an existing Job Execution. This operation returns
	 *            nothing. This operation does not require a request body.
	 * @return 
	 */
	public boolean deleteJobExecutions(String jobExecutionId) {
		RestCall caller = new RestCall();
		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/job-executions/" + jobExecutionId, null, "DELETE", token);
		try {
			if (connection.getResponseCode() == 204)
				return true;
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

}
